package com.lyq.demo.redis.controller;

import com.lyq.demo.redis.entity.Order;
import com.lyq.demo.redis.entity.User;
import com.lyq.demo.redis.service.OrderService;
import com.lyq.demo.redis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lyq
 * @time 2019/5/11 20:35
 */
@RestController
public class DemoController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;

    /**
     * 发送主题消息
     * @param msg
     * @return
     */
    @GetMapping("/topic/{msg}")
    public String sendTopicMsg(@PathVariable("msg") String msg){
        redisTemplate.convertAndSend("register",msg);
        return "success";
    }

    /**
     * 发送队列消息
     * @param msg
     * @return
     */
    @GetMapping("/queue/{msg}")
    public String sendQueueMsg(@PathVariable("msg") String msg){
        redisTemplate.opsForList().leftPush("list1",msg);
        return "success";
    }

    @GetMapping("/user/{id}")
    public User getById(@PathVariable("id") Integer id){
        return userService.getById(id);
    }

    @GetMapping("/user/{id}/{name}")
    public User updateById(@PathVariable("id") Integer id,@PathVariable("name") String name){
        return userService.updateById(id,name);
    }

    @GetMapping("/duser/{id}")
    public String deleteById(@PathVariable("id") Integer id){
        userService.deleteById(id);
        return "success";
    }

    @GetMapping("/order/{id}")
    public Order getById2(@PathVariable("id") Integer id){
        return orderService.getById(id);
    }

    @GetMapping("/order/{id}/{name}")
    public Order updateById2(@PathVariable("id") Integer id,@PathVariable("name") String name){
        return orderService.updateById(id,name);
    }

    @GetMapping("/dorder/{id}")
    public String deleteById2(@PathVariable("id") Integer id){
        orderService.deleteById(id);
        return "success";
    }
}
